package com.topscomm.qem;

import com.topscomm.basic.exception.ServiceException;
import com.topscomm.cbo.pojo.CboMessageAppEntity;
import com.topscomm.cbo.pojo.CboMessageSysEntity;
import com.topscomm.cbo.service.ICboMessageAppService;
import com.topscomm.cbo.service.ICboMessageSysService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.TapService;
import com.topscomm.tap.common.DatacodeCache;
import com.topscomm.tap.common.SessionUserBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;
import java.util.function.UnaryOperator;

@SuppressWarnings("rawtypes")
public class QemService<T extends QemEntity, TDao extends IQemDao> extends TapService<T, TDao> implements IQemService<T, TDao> {
    @Autowired
    private ICboMessageAppService cboMessageAppService;
    @Autowired
    private ICboMessageSysService cboMessageSysService;
    @Autowired
    private DatacodeCache datacodeCache;

    /**
     * 统一的给扩充字段赋值，第一个为单号，第二个为申请部门，第三个位申请人
     */
    @Override
    protected void afterSubmit(TapEntity entity, SessionUserBean sessionUserBean) {
        super.afterSubmit(entity, sessionUserBean);
        try {
            StringBufferProxy sql = new StringBufferProxy();
            sql.appendLineSingle("update CboApprovalFlowIns set ");
            sql.appendLineSingle("extendfield1 = (select docno from {0} "
                    + "where CboApprovalFlowIns.sourceid = {0}.id "
                    + "and CboApprovalFlowIns.sourcetype = '{0}'),", entity.getTableName());
            sql.appendLineSingle("extendfield2 = (select deptcode from {0} "
                    + "where CboApprovalFlowIns.sourceid = {0}.id "
                    + "and CboApprovalFlowIns.sourcetype = '{0}'),", entity.getTableName());
            sql.appendLineSingle("extendfield3 = (select usercode from {0} "
                    + "where CboApprovalFlowIns.sourceid = {0}.id "
                    + "and CboApprovalFlowIns.sourcetype = '{0}')", entity.getTableName());
            sql.appendLineSingle("where sourceid = '{0}' and sourcetype='{1}'", entity.getId(), entity.getTableName());
            this.getDao().updateExecute(sql.toString());
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    /**
     * @description: 生成系统通知Map
     * @author: chengengwei
     * @date: 2021-01-04
     * @modify: extendfield1 为title
     */
    @Override
    public CboMessageSysEntity createSysMessageMap(String senderUserCode, String senderUserName, String receiveUserCode, String receiveUserName, String title, String content, long sourceid,
                                                   String sourcetype) {
        CboMessageSysEntity sysMessage = new CboMessageSysEntity();
        sysMessage.setSourceid(sourceid);
        sysMessage.setSourcetype(sourcetype);
        sysMessage.setSendercode(senderUserCode);
        sysMessage.setSendername(senderUserName);
        sysMessage.setReceivercode(receiveUserCode);
        sysMessage.setReceivername(receiveUserCode);
        sysMessage.setSubject(title);
        sysMessage.setContent(content);
        return sysMessage;
    }

    /**
     * @param sendtype app发送方式 0 为 单推 1 为 allpush AppSendTypeEnum
     * @description: 生成APP通知Map
     * @author: chengengwei
     * @date: 2021-01-04
     * @modify:
     */
    @Override
    public CboMessageAppEntity createAppMessageMap(String usercode, String subject, String content,
                                                   long sourceid, String sourcetype, int sendtype) {
        String userid = datacodeCache.getValueDynamic("CboUser", usercode, "code", "id");
        CboMessageAppEntity appMessage = new CboMessageAppEntity();
        appMessage.setReceiverid(ConvertUtil.convertToLong(userid));
        appMessage.setContent(content);
        appMessage.setSourceid(sourceid);
        appMessage.setSourcetype(sourcetype);
        appMessage.setSendtype(sendtype);
        return appMessage;
    }

    /**
     * 用来处理重复拼串
     *
     * @param
     * @return
     * @throws
     * @author xinfushuo
     * @date 2022/11/24
     * @modify
     */
    @Override
    public StringBufferProxy appendSql(Map<String, Object> paraMap, StringBufferProxy sql, String paramName, String template, UnaryOperator<String> handleParam) {
        if (paraMap.containsKey(paramName)) {
            String value = paraMap.get(paramName).toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(template, handleParam.apply(value));
            }
        }
        return sql;
    }

    /**
     * @param mapRow        实体属性map
     * @param fieldName     字段名称
     * @param dispFieldName 字段显示值名称
     * @return 处理后的显示值
     * @throws
     * @author xinfushuo
     * @date 2023/1/9
     * @modify
     */
    @Override
    public void setDispField(Map<String, Object> mapRow, String fieldName, String dispFieldName, UnaryOperator<String> handleField) {
        if (mapRow.get(fieldName) != null) {
            String temp = mapRow.get(fieldName).toString();
            mapRow.put(dispFieldName, handleField.apply(temp));
        }
    }

    /**
      * 为不传disp字段名称的默认处理展示值
      * @author xinfushuo
      * @param
      * @throws
      * @return
      * @date 2023/1/9
      * @modify
      */
    @Override
    public void setDispField(Map<String, Object> mapRow, String fieldName, UnaryOperator<String> handleField) {
        String dispPrefix = "disp";
        setDispField(mapRow,fieldName,dispPrefix+fieldName,handleField);
    }

}
